package com.qc31.comlib.popup

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.qc31.comlib.R
import com.qc31.comlib.adapter.KeyValueAdapter
import com.qc31.comlib.entity.KeyValue

/** @Author Jasper @Time 2020/9/21 @Desc */
class KeyValuePopup(val context: Context, val title: String = "") : PopupWindow(context) {
    private var recyclerView: RecyclerView? = null
    private var titleText: TextView? = null
    private var mAdapter: KeyValueAdapter? = null


    init {
        contentView = LayoutInflater.from(context).inflate(R.layout.popup_key_value, null)
        initTextView()
        initRecyclerVIew()
        isFocusable = true //popupwindow设置焦点
        setBackgroundDrawable(ColorDrawable(-0x56000000)) //设置背景
//        isOutsideTouchable = true //点击外面窗口消失
        width = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的宽
        height = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的高
    }

    private fun initRecyclerVIew() {
        recyclerView = contentView.findViewById(R.id.recyclerWrap)
        recyclerView?.run {
            layoutManager = LinearLayoutManager(context)
            mAdapter = KeyValueAdapter().apply {
                setOnItemClickListener { adapter, view, position ->
                    index = position
                    notifyDataSetChanged()
                }
            }
            adapter = mAdapter
        }
    }


    private fun initTextView() {
        titleText = contentView.findViewById(R.id.tvKeyValueTitle)
        titleText?.text = title
        contentView.findViewById<TextView>(R.id.tvSure).setOnClickListener {
            mAdapter?.run { callBack?.onBack(data[index], index) }
            dismiss()

        }
        contentView.findViewById<ImageView>(R.id.ivClose).setOnClickListener {
            dismiss()
        }
    }

    //****************************showPopupWindow**************************************
    fun showPopupWindow(
        view: View,
        title: String,
        data: MutableList<KeyValue>,
        position: Int
    ) {
        //相对于父控件位置
        showAtLocation(view, Gravity.CENTER, 0, 0)
        titleText?.text = title
        mAdapter?.index = position
        mAdapter?.setList(data)
        mAdapter?.notifyDataSetChanged()

    }

    fun showPopupWindow(
        view: View,
        data: MutableList<KeyValue>,
        position: Int
    ) {
        mAdapter?.index = position
        mAdapter?.setList(data)
        mAdapter?.notifyDataSetChanged()
        //相对于控件位置
        showAsDropDown(view)
    }

    fun showAtLocation(
        view: View,
        data: MutableList<KeyValue>,
        position: Int
    ) {
        mAdapter?.index = position
        mAdapter?.setList(data)
        mAdapter?.notifyDataSetChanged()
        showAtLocation(view, Gravity.CENTER, 0, 0);
    }

    fun showAtLocation(
        view: View,
        title: Int,
        data: MutableList<KeyValue>,
        position: Int
    ) {
        titleText?.setText(title)
        mAdapter?.index = position
        mAdapter?.setList(data)
        mAdapter?.notifyDataSetChanged()
        showAtLocation(view, Gravity.CENTER, 0, 0);
    }

    //****************************CallBack**************************************
    private var callBack: CallBack? = null

    interface CallBack {
        fun onBack(entity: KeyValue, index: Int)
    }

    fun setCallBack(callBack: CallBack) {
        this.callBack = callBack
    }

}